home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 9 / The PC-SIG Library on CD ROM - Ninth Edition.iso / 1501_600 / DISK1533 / DISK1533.ZIP / PR.C < prev    next >
Text File  |  1989-02-22  |  4KB  |  134 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. #define    TRUE    1
  5. #define    FALSE    0
  6. #define MAXCOL    255
  7. #define DPGSIZ    58
  8. #define DHDLEN    3
  9.  
  10. static    char    OBufr[BUFSIZ];        /* cause sysprint to buffer */
  11. static    char    IBufr[BUFSIZ];        /* cause sysin to buffer    */
  12.  
  13. static    char    *PROGRAM[] = { "S. Leoce PR filter",
  14.                    "V1.0 R1.0 SVCLVL 0" };
  15. #include "b:cmdline.c"
  16.  
  17. int page (unsigned int, char *, char *, short unsigned int *);
  18.  
  19. main (argc, argv)
  20. int    argc;
  21. char   *argv[];        /* the command line is here    */
  22.  
  23. {
  24.     static    char    line[MAXCOL];        /* input line ...       */
  25.     char    *filename  = "SYSIN";        /* the default stream input*/
  26.     short int PageSize = DPGSIZ;        /* 58 lines / page default */
  27.     short int Verbose  = FALSE;        /* no talkies as go ...       */
  28.     short int LinesNow = 0;            /* lines output so far       */
  29.     
  30.      short unsigned int LineOption = FALSE;     /* change option present   */
  31.      short unsigned int number = FALSE;      /* number output lines?    */
  32.      short unsigned int Feed = FALSE;    /* allow first form feed?  */
  33.      short unsigned int Burst = FALSE;    /* burst page required     */
  34.         unsigned long  int linect = 0L;        /* no output lines yet     */
  35.         unsigned int       pagect = 0;        /* pagecounter           */
  36.         short int col = MAXCOL;            /* columns to be a line    */
  37.  
  38.     long    secs_now = time(&secs_now);
  39.     char    *stamp  = ctime(&secs_now);
  40.  
  41.     opterr = FALSE;        /* this program handles errors           */
  42.     while ((LineOption = getopt(argc, argv, "BbFfL:l:NnVvC:c:")) != EOF) {
  43.         switch (LineOption) {
  44.              case '?':
  45.                 fprintf(stderr,"Invalid option %c\n", badopt);
  46.                 fprintf(stderr,"usage: pr [/BFNVLn Cn] [file]\n");
  47.                 _exit (0xff);
  48.             case 'L':
  49.             case 'l': PageSize = atoi(optarg) + DHDLEN;
  50.                   if (PageSize < 1)
  51.                     PageSize = DPGSIZ;
  52.                   break;
  53.             case 'V':
  54.             case 'v': Verbose = TRUE;
  55.                   break;
  56.             case 'N':
  57.             case 'n': number = TRUE;
  58.                   break;
  59.             case 'C':
  60.             case 'c': col = atoi(optarg);
  61.                   if (col <= 0 || col > MAXCOL)
  62.                     col = MAXCOL;
  63.                   break;
  64.             case 'F':    /* cancel first form-feed */
  65.             case 'f':
  66.                   Feed = TRUE;
  67.                   break;
  68.             case 'B':    /* supply a burst page */
  69.             case 'b':
  70.                   Burst = TRUE;
  71.                   break;
  72.         };
  73.     };
  74.     if (argv [optind] != NULL) {
  75.         if(freopen(argv[optind], "r", stdin) == NULL) {
  76.             fprintf(stderr, "pr: no such file %s\n",argv[optind]);
  77.             exit(0x20);
  78.         };
  79.         filename = strupr(argv[optind]);
  80.     };
  81.  
  82.     LinesNow = PageSize - 1;
  83.  
  84.     if( Burst ) {
  85.         if( Feed )
  86.             fprintf( stdout, "\f");
  87.         fprintf( stdout, "\n\n\n\n\n\n\n\n\n\n\tFile   : %s\n\t"
  88.                 "Printed: %s", filename, stamp );
  89.         if( !Feed )
  90.             fprintf( stdout, "\f");
  91.     }
  92.         
  93.     while (gets(line) != NULL) {
  94.         ++linect;
  95.         line[col] = '\0';     /* terminate long ones here ...    */
  96.         if (++LinesNow == PageSize)
  97.             LinesNow = page(++pagect, stamp, filename, &Feed);
  98.         if (number)
  99.             printf("%5ld ", linect);
  100.         if(puts(line) == EOF)    {    /* no ball room to write */
  101.             fprintf(stderr,"IELPUT01 Device denied write\n");
  102.             _exit(0x36);
  103.         };
  104.     };
  105.  
  106.     if (fflush(stdout) == EOF)
  107.         fprintf(stderr, "pr: couldn't flush sysprint");
  108.  
  109.     if(Verbose) {
  110.         fprintf(stderr, "pr: ");
  111.         if (linect == 0)
  112.                     fprintf(stderr, "SYSIN empty\n");
  113.         else
  114.                     fprintf(stderr,"%ld lines on %d pages\n",linect,pagect);
  115.     };
  116.     exit(0x00);
  117. }
  118.  
  119. int page (pagect, stamp, file, Feed )
  120. unsigned pagect;
  121. char *stamp;
  122. char *file;
  123. short unsigned int * Feed;
  124. {
  125.     if( *Feed )
  126.         fprintf( stdout, "\f" );
  127.  
  128.     fprintf(stdout,"Page %02u  %-35s  Printed %s\n\n",
  129.              pagect,file,stamp);
  130.  
  131.     *Feed = TRUE;      /* after first page, feeding is required */
  132.      
  133.     return (DHDLEN);  /* number of lines already used up  */
  134. }